package com.qx.chat.system.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qx.chat.system.dao.AbstractRepository;
import com.qx.chat.system.entity.DeptUser;
import com.qx.chat.system.entity.Group;
import com.qx.chat.system.entity.GroupUser;
import com.qx.chat.system.entity.User;
import com.qx.chat.system.util.JsonUtil;

/**
 * TODO
 * 
 * @author cyf
 * @version TODO
 */
@Component
public class GroupServiceImpl implements GroupService {
	// @Autowired
	// private AbstractRepository<User> userDaoImpl;
	@Autowired
	private AbstractRepository<Group> groupDaoImpl;
	@Autowired
	private AbstractRepository<GroupUser> groupUserDaoImpl;
	@Autowired
	private AbstractRepository<DeptUser> deptUserDaoImpl;
	@Autowired
	private AbstractRepository<User> userDaoImpl;

	/**
	 * 新增或编辑群组
	 * 
	 * @param userId
	 * @return
	 */
	public Group createOrUpdate(Group group, String ownerLoginName) {
		User user = (User) userDaoImpl.findOne("loginName", ownerLoginName);
		String groupId = group.getId();
		if (user != null) {
			Group gp = groupDaoImpl.findById(groupId);
			if (gp == null) {
				DeptUser du = (DeptUser) deptUserDaoImpl.findOne("user", user.getId());
				gp = new Group();
				gp.setName(group.getName());
				gp.setIntro(group.getIntro());
				gp.setOwner(user);
				gp.initData(user, du.getDept());
				groupDaoImpl.insert(gp);
				GroupUser gu = new GroupUser();
				gu.setUser(user);
				gu.setGroup(gp);
				gu.setOrder(0);
				groupUserDaoImpl.insert(gu);
			} else {
				gp.setName(group.getName());
				gp.setIntro(group.getIntro());
				groupDaoImpl.getMongoTemplate().save(gp);
			}
			return gp;
		} 
		return null;
	}

	/**
	 * 读取群组
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public String view(String groupId) {
		Group group = groupDaoImpl.findById(groupId);
		List<GroupUser> gus = groupUserDaoImpl.findList("group.id", groupId);
		List<User> members = new ArrayList<User>();
		for (Iterator<GroupUser> iterator = gus.iterator(); iterator.hasNext();) {
			GroupUser groupUser = (GroupUser) iterator.next();
			members.add(groupUser.getUser());
		}
		JSONObject jgroup = JsonUtil.objectToJsonObject(group);
		JSONArray jmembers = JsonUtil.listToJsonArray(members);
		JSONObject jo = new JSONObject();
		jo.put("group", jgroup);
		jo.put("members", jmembers);
		return jo.toJSONString();
	}

	/**
	 * 解散群组
	 * 
	 * @param userId
	 * @return
	 */
	@Override
	public String dissolve(String groupId) {
		groupUserDaoImpl.removeByKey("group.id", groupId);
		groupDaoImpl.removeById(groupId);
		JSONObject jo = new JSONObject();
		jo.put("result", true);
		return jo.toJSONString();
	}
	/**
	 * 群组添加成员
	 * @param userId
	 * @return
	 */
	public String addMembers(String groupId,String memberIds){
		List<GroupUser> gus = groupUserDaoImpl.findList("group.id", groupId);
		List<String> umap = new ArrayList<String>();
		if(gus!=null){
			for (Iterator<GroupUser> iterator = gus.iterator(); iterator.hasNext();) {
				GroupUser groupUser = (GroupUser) iterator.next();
				umap.add(groupUser.getUser().getId());
			}
		}
		Group gp = groupDaoImpl.findById(groupId);
		String[] ms = memberIds.split(";");
		List<User> members = new ArrayList<User>();
		for (int i = 0; i < ms.length; i++) {
			User user = (User) userDaoImpl.findById(ms[i]);
			if(user!=null&&!umap.contains(user.getId())){
				GroupUser gu = new GroupUser();
				gu.setUser(user);
				gu.setGroup(gp);
				gu.setOrder(0);
				groupUserDaoImpl.insert(gu);
				members.add(user);
			}
		}
		JSONObject jo = new JSONObject();
		JSONArray jmembers = JsonUtil.listToJsonArray(members);
		jo.put("members", jmembers);
		jo.put("result", true);
		return jo.toJSONString();
	}
}
